import Vue from 'vue'

import VueRouter from 'vue-router'
import HomeCon from '@/view/Home/Home.vue'
import ShopcarCon from '@/view/shopcar/Shopcar.vue'
import Category from '@/view/Categories/Category.vue'
import GoodsList from '@/view/goods/GoodsList.vue'
import GoodsInfo from '@/view/goods/GoodsInfo.vue'
import Search from '@/components/search.vue'
// import User from '@/view/User.vue'
Vue.use(VueRouter)

var router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/home',
      name: 'home',
      meta: { title: '首页' }
    },
    {
      path: '/home',
      component: HomeCon,
      meta: { title: '首页' }
    },
    {
      path: '/shopcar',
      name: 'shopcar',
      component: ShopcarCon,
      meta: { title: '购物车' }
    },
    {
      path: '/category',
      name: 'category',
      component: Category,
      meta: { title: '分类' }
    },
    {
      path: '/goodslist/id=:category_id',
      component: GoodsList,
      props: true,
      name: 'goods_list',
      meta: { title: '商品列表' }
    },
    {
      // path: '/goodsinfo/goods_id=:goods_id',
      path: '/goodsinfo',
      component: GoodsInfo,
      props: true,
      name: 'goods_info',
      meta: { title: '商品信息' }
    },
    {
      path: '/search',
      name: 'Search',
      component: Search
    },
    {
      path: '/user',
      name: 'user',
      component: () => import('../view/User.vue'),
      meta: { title: '我的' }
    },
    {
      path: '/user/login',
      name: 'login',
      component: () => import('../view/user/Login.vue'),
      meta: { title: '登录' }
    },
    {
      path: '/user/register',
      name: 'register',
      component: () => import('../view/user/Register.vue'),
      meta: { title: '注册' }
    }
  ],
  // 激活底部标签栏样式
  linkActiveClass: 'mui-active'
})
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
    next()
  }
  if (to.meta.title === '购物车') {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/user/login')
    }
  }
  next()
})

export default router
